#include "ldebug.h"

#include <stdarg.h>
#include <QDateTime>

LDebug::LDebug(QIODevice *out, QIODevice *err)
{
    m_out = out;
    if (!err) m_err = out;
    else m_err = err;
}

QString LDebug::gen(const char *fmt, const char *prefix, va_list argp) {
    QString d;
    d.vsprintf(fmt,argp);

    QDateTime now = QDateTime::currentDateTime();

    d = QString("[%1 %2] ").arg(prefix,now.toString("MM-dd hh:mm")) + d + "\n";
    return d;
}

void LDebug::log(const char *fmt, ...){
    va_list argp;

    va_start(argp,fmt);
    m_out->write(gen(fmt,"LOG",argp).toLocal8Bit());
    va_end(argp);
}

void LDebug::warn(const char *fmt, ...) {
    va_list argp;

    va_start(argp,fmt);
    m_out->write(gen(fmt,"WARN",argp).toLocal8Bit());
    va_end(argp);
}

void LDebug::error(const char *fmt, ...) {
    va_list argp;

    va_start(argp,fmt);
    m_out->write(gen(fmt,"ERROR",argp).toLocal8Bit());
    va_end(argp);
}


